package com.typing.alarm.activity;

import com.typing.alarm.R;
import com.typing.alarm.service.ITypingAlarmService;
import com.typing.alarm.service.TypingAlarmService;
import com.typing.alarm.value.TypingAlarmVO;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AlarmRingingActivity extends Activity {
	
	public ServiceConnection conn=null;
	private TextView label;
	private EditText input;
	private Button btn;
	private TypingAlarmVO alarmInfo;
	private boolean isFirst = false;
	private LinearLayout typingArea;
	private long startTime;
	private long endTime;
	int count=0;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.alarm_ringing);
        alarmInfo = (TypingAlarmVO)getIntent().getParcelableExtra("alarm_info");
        label = (TextView)findViewById(R.id.label_alarm_text);
        input = (EditText)findViewById(R.id.input_alarm_text);
        btn = (Button)findViewById(R.id.btn_typing_start);
        typingArea = (LinearLayout)findViewById(R.id.typing_area);
        
        
        
        label.setText(alarmInfo.getTypingText());
        btn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub	
				showOrHideTypingArea(true);							
			}
        	
        });
        
        input.setOnKeyListener(new OnKeyListener(){
        	
			@Override
			public boolean onKey(View view, int arg1, KeyEvent event) {
				switch(event.getAction()){
					case KeyEvent.ACTION_DOWN:					
						if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
							// TODO Auto-generated method stub
							label = (TextView)findViewById(R.id.label_alarm_text);
					        input = (EditText)findViewById(R.id.input_alarm_text);
					        if(label.getText().toString().equals(input.getText().toString())){
					        	endTime = System.currentTimeMillis();
					        	Log.d("endtime",""+(endTime - startTime));
					        	
					        	int totalByte = label.getText().toString().getBytes().length;
					        	Log.d("totalByte",""+((totalByte*60000)/(endTime - startTime)));
					        	if((((totalByte+5)*60000)/(endTime - startTime)) >= Integer.parseInt(alarmInfo.getTypingSpeed()) ){			        	            	
					   				bindService(new Intent(AlarmRingingActivity.this,TypingAlarmService.class) ,conn, Context.BIND_AUTO_CREATE);
					   				NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
					   				nm.cancel(1234);		   				
					   				startActivity(new Intent(AlarmRingingActivity.this,AlarmListActivity.class));
					        	}else{
					        		Toast.makeText(AlarmRingingActivity.this, "타수 미달 !!  다시 도전하세요.", Toast.LENGTH_SHORT).show();
					        		input.setText("");
					        		showOrHideTypingArea(false);
					        	}
					        }else{
					        	Toast.makeText(AlarmRingingActivity.this, "오타  !!  다시 도전하세요.", Toast.LENGTH_SHORT).show();
					        	input.setText("");
					        	showOrHideTypingArea(false);
					        }		
					        return true;
						}else{
							return false;
						}
					case KeyEvent.ACTION_UP:
						return false;
					default:
						return false;
				}
				
			}        	
        });
        
        conn=new ServiceConnection() {
        	@Override
        	public void onServiceConnected(ComponentName name, IBinder service){  
        		// TODO Auto-generated catch block
        		ITypingAlarmService myService = ITypingAlarmService.Stub.asInterface(service);
        		try {        			
        			myService.stopRepeat(alarmInfo.getAlarmId());	        			
				} catch (RemoteException e) {					
					e.printStackTrace();
				}
        	}
        	@Override
        	public void onServiceDisconnected(ComponentName name){
        		// TODO Auto-generated catch block
        	}			
        };
        
	}
	public void showOrHideTypingArea(boolean isVisible){
    	if(isVisible){
    		typingArea.setVisibility(LinearLayout.VISIBLE);
			input.requestFocus();
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
	        imm.showSoftInput(input, 0);
	        
			btn.setVisibility(Button.INVISIBLE);
			startTime = System.currentTimeMillis();	
    	}else{
    		typingArea.setVisibility(LinearLayout.INVISIBLE);			
			btn.setVisibility(Button.VISIBLE);
    	}
    }
}
